{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# circuit"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/circuit.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/circuit.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Decomposition of the circuit constraint in Google CP Solver.\n",
    "\n",
    "\n",
    "  Cf Global constraint catalog:\n",
    "  http://www.emn.fr/x-info/sdemasse/gccat/Ccircuit.html\n",
    "\n",
    "  Solution of n=4:\n",
    "  x: [2, 0, 3, 1]\n",
    "  x: [3, 0, 1, 2]\n",
    "  x: [1, 3, 0, 2]\n",
    "  x: [3, 2, 0, 1]\n",
    "  x: [1, 2, 3, 0]\n",
    "  x: [2, 3, 1, 0]\n",
    "\n",
    "  The 'orbit' method that is used here is based on some\n",
    "  observations on permutation orbits.\n",
    "\n",
    "  Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/circuit_test.mzn\n",
    "  * Gecode: http://www.hakank.org/gecode/circuit_orbit.mzn\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "#\n",
    "# circuit(x)\n",
    "# constraints x to be an circuit\n",
    "#\n",
    "# Note: This assumes that x is has the domain 0..len(x)-1,\n",
    "#       i.e. 0-based.\n",
    "#\n",
    "\n",
    "\n",
    "def circuit(solver, x):\n",
    "  n = len(x)\n",
    "  z = [solver.IntVar(0, n - 1, \"z%i\" % i) for i in range(n)]\n",
    "\n",
    "  solver.Add(solver.AllDifferent(x))\n",
    "  solver.Add(solver.AllDifferent(z))\n",
    "\n",
    "  # put the orbit of x[0] in in z[0..n-1]\n",
    "  solver.Add(z[0] == x[0])\n",
    "  for i in range(1, n - 1):\n",
    "    # The following constraint give the error\n",
    "    # \"TypeError: list indices must be integers, not IntVar\"\n",
    "    # solver.Add(z[i] == x[z[i-1]])\n",
    "\n",
    "    # solution: use Element instead\n",
    "    solver.Add(z[i] == solver.Element(x, z[i - 1]))\n",
    "\n",
    "  #\n",
    "  # Note: At least one of the following two constraint must be set.\n",
    "  #\n",
    "  # may not be 0 for i < n-1\n",
    "  for i in range(1, n - 1):\n",
    "    solver.Add(z[i] != 0)\n",
    "\n",
    "  # when i = n-1 it must be 0\n",
    "  solver.Add(z[n - 1] == 0)\n",
    "\n",
    "\n",
    "def main(n=5):\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Send most money\")\n",
    "\n",
    "  # data\n",
    "  print(\"n:\", n)\n",
    "\n",
    "  # declare variables\n",
    "  # Note: domain should be 0..n-1\n",
    "  x = [solver.IntVar(0, n - 1, \"x%i\" % i) for i in range(n)]\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  circuit(solver, x)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  solution = solver.Assignment()\n",
    "  solution.Add(x)\n",
    "\n",
    "  collector = solver.AllSolutionCollector(solution)\n",
    "\n",
    "  solver.Solve(\n",
    "      solver.Phase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE),\n",
    "      [collector])\n",
    "\n",
    "  num_solutions = collector.SolutionCount()\n",
    "  for s in range(num_solutions):\n",
    "    print(\"x:\", [collector.Value(s, x[i]) for i in range(len(x))])\n",
    "\n",
    "  print()\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "  print()\n",
    "\n",
    "\n",
    "n = 5\n",
    "if len(sys.argv) > 1:\n",
    "  n = int(sys.argv[1])\n",
    "\n",
    "main(n)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
